﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using RobotCtrl;

namespace RobotCtrl
{
    public class World
    {
        private static ObstacleMap obstacleMap;
        private static Robot robot;
        private int gridX;
        private int gridY;
        private int zeroX;
        private int zeroY;

        public static ObstacleMap ObstacleMap { get { return obstacleMap; } set { obstacleMap = value; } }

        public static Robot Robot
        {
            get { return robot; }
            set { robot = value; }
        }
                
        public int GridX
        {
            get { return gridX; }
            set { gridX = value; }
        }

        public int GridY
        {
            get { return gridY; }
            set { gridY = value; }
        }

        public int ZeroX
        {
            get { return zeroX; }
            set { zeroX = value; }
        }

        public int ZeroY
        {
            get { return zeroY; }
            set { zeroY = value; }
        }

        public World()
        {
        }

        public World(Robot bot, int gridX, int gridY, int zeroX, int zeroY)
        {
            robot = bot;
            this.gridX = gridX;
            this.gridY = gridY;
            this.zeroX = zeroX;
            this.zeroY = zeroY;
        }

        public static double GetFreeSpace()
        {
            DrivePosition offset = robot.RelativeRadarPosition;
            DrivePosition pos = robot.Position;
            double phi = pos.Angle / 180.0 * Math.PI;
            DrivePosition radarPos = new DrivePosition(
                pos.X + offset.X * Math.Cos(phi) - offset.Y * Math.Sin(phi),
                pos.Y + offset.X * Math.Sin(phi) + offset.Y * Math.Cos(phi),
                (pos.Angle + offset.Angle) % 360);
            return obstacleMap.GetFreeSpace(radarPos);
        }

    }
}
